package com.store.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.bean.GoldLogBean;

import java.util.List;

/**
 * 金币明细适配器
 */
public class GoldrecordListAdapter extends BaseAdapter {
    private Context context;        //上下文对象引用
    private List<GoldLogBean> data;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public GoldrecordListAdapter(Context context, List<GoldLogBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }


    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.tokenrecordlist, null);
            viewHolder.amount = (TextView) convertView.findViewById(R.id.amount);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            viewHolder.detail = (TextView) convertView.findViewById(R.id.detail);
            convertView.setTag(viewHolder);            //表示给View添加一个格外的数据，
        } else {
            viewHolder = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来
        }
        GoldLogBean goldLogBean = data.get(position);
        String symbol;
        String value = goldLogBean.getAmount();
        if (!TextUtils.isEmpty(value)) {
            value = value.split("\\.")[0];
            if (value.contains("-")) {
                symbol = "";
//					viewHolder.amount.setTextColor(Color.parseColor("#8cc83a"));
            } else {
//					viewHolder.amount.setTextColor(Color.parseColor("#f96116"));
                symbol = "+";
            }
            viewHolder.amount.setText(symbol + value);
        }
        String date = goldLogBean.getTracked_date();
        if (!TextUtils.isEmpty(date)) {
            viewHolder.time.setText(date);
        }
        String detalStr = goldLogBean.getDetail();
        if (!TextUtils.isEmpty(detalStr)) {
            viewHolder.detail.setText(detalStr);

        }
        return convertView;
    }

    /**
     * 定义一个内部类
     * 声明相应的控件引用
     */
    public class ViewHolder {

        public TextView amount;            //金额
        public TextView time;            //时间
        public TextView detail;    //属性（消费还是充值）
    }
}
